<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :xs="24">
        <el-card style="border-radius: 10px">
          <div class="head-container">
            <el-input
              v-model="drawingTypeName"
              placeholder="请输入分类名称"
              clearable
              size="small"
              prefix-icon="el-icon-search"
              style="margin-bottom: 20px"
            />
          </div>
          <div class="head-container">
            <el-tree
              :data="drawingTypeOptions"
              :props="defaultProps"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              ref="tree"
              default-expand-all
              @node-click="handleNodeClick"
              :indent="5"
            />
          </div>
        </el-card>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-card style="border-radius: 10px">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
            <el-form-item label="产品名称" prop="itemName">
              <el-input
                v-model="queryParams.itemName"
                placeholder="请输入关联产品名称"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="产品图号" prop="itemDrawCode">
              <el-input
                v-model="queryParams.itemDrawCode"
                placeholder="请输入关联产品图号"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="图纸集名称" prop="drawingName">
              <el-input
                v-model="queryParams.drawingName"
                placeholder="请输入图纸集名称"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>

            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
        <div style="height: 15px"></div>
        <el-card style="border-radius: 10px">
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="success"
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['md:drawing:add']"
              >新增</el-button>
            </el-col>

            <el-col :span="1.5">
              <el-button
                type="primary"
                icon="el-icon-search"
                size="mini"
                @click="handleQueryLine"
                v-hasPermi="['md:drawing:remove']"
              >图纸搜索</el-button>
            </el-col>


            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="drawingList" border>
            <el-table-column label="产品名称" align="center" prop="itemName" />
            <el-table-column label="产品图号" align="center" prop="itemDrawCode" />
            <el-table-column label="图纸分类名称" align="center" prop="drawingTypeName" />
            <el-table-column label="图纸集名称" align="center" prop="drawingName">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  size="small"
                  @click="handleView(scope.row)"
                  v-hasPermi="['md:drawing:query']"
                >{{ scope.row.drawingName }}</el-button>
              </template>
            </el-table-column>
            <el-table-column label="图纸集版本" align="center" prop="version" />
            <el-table-column label="工艺图纸集授权" align="center" prop="processAuthorization">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.processAuthorization"
                  :active-value="1"
                  :inactive-value="0"
                  @change="handleProcessAuthorizationChange(scope.row)"
                />
              </template>
            </el-table-column>
            <el-table-column label="bom图纸集授权" align="center" prop="bomAuthorization">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.bomAuthorization"
                  :active-value="1"
                  :inactive-value="0"
                  @change="handleBomAuthorizationChange(scope.row)"
                />
              </template>
            </el-table-column>
            <el-table-column label="图纸集状态" align="center" prop="status">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.status === 0" type="info">草稿</el-tag>
                <el-tag v-else-if="scope.row.status === 1" type="success">启用</el-tag>
                <el-tag v-else-if="scope.row.status === 2" type="danger">禁用</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                  <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleUpdate(scope.row)"
                      v-hasPermi="['md:drawing:edit']"
                      v-if="scope.row.status === 0"
                  >修改</el-button>
                  <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-circle-check"
                      @click="handleStart(scope.row)"
                      v-hasPermi="['md:drawing:edit']"
                      v-if="scope.row.status !== 1"
                  >启用</el-button>
                  <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-circle-close"
                      @click="handleStop(scope.row)"
                      v-hasPermi="['md:drawing:edit']"
                      v-if="scope.row.status === 1"
                  >禁用</el-button>
                  <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelete(scope.row)"
                      v-hasPermi="['md:drawing:remove']"
                      v-if="scope.row.status === 0"
                  >删除</el-button>
<!--                  <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-view"
                      @click="handleViewUsed(scope.row)"
                      v-hasPermi="['md:drawing:view']"
                  >查用</el-button>-->

              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-card>
      </el-col>
    </el-row>


    <!-- 添加或修改图纸集管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-card style="border-radius: 10px">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="图纸分类" prop="drawingTypeId">
                <treeselect v-model="form.drawingTypeId" :options="drawingTypeOptions" :show-count="true" disabled
                            v-if="optType == 'view'"/>
                <treeselect v-model="form.drawingTypeId" :options="drawingTypeOptions" :show-count="true"
                            placeholder="请选择所属分类" v-else :disable-branch-nodes="true"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="图纸集名称" prop="drawingName">
                <el-input v-model="form.drawingName" placeholder="请输入产品图纸集名称" :readonly="optType === 'view'" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="产品编码" prop="itemCode">
                <el-input v-model="form.itemCode" readonly>
                  <el-button
                      slot="append"
                      @click="handleSelectProduct"
                      icon="el-icon-search"
                      v-if="optType === 'add'">
                  </el-button>
                </el-input>
              </el-form-item>
              <ItemSelect ref="itemSelect" @onSelected="onItemSelected" > </ItemSelect>
            </el-col>
            <el-col :span="12">
              <el-form-item label="产品名称" prop="itemName">
                <el-input v-model="form.itemName" readonly />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="产品图号" prop="itemDrawCode">
                <el-input v-model="form.itemDrawCode" readonly />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="产品单位" prop="unitOfMeasure">
                <el-input v-model="form.unitOfMeasure" readonly />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="产品规格型号" prop="specification">
                <el-input v-model="form.specification" type="textarea" readonly />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="设计人" prop="designer">
                <el-input v-model="form.designer" placeholder="请输入设计人" :readonly="optType === 'view'"  />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="设计时间" prop="designDate">
                <el-date-picker clearable
                                v-model="form.designDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                :disabled="optType === 'view'"
                                placeholder="请选择设计时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item label="描述" prop="remark">
                <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :readonly="optType === 'view'" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>

      <el-divider content-position="center" class="custom-divider" v-if="optType !== 'add'">图纸信息</el-divider>

      <el-card style="border-radius: 10px" v-if="optType !== 'add'">
        <drawing-line ref="line" :drawingId="form.id" :optType="optType" v-if="optType !== 'add'"></drawing-line>
      </el-card>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="optType !== 'view'">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 图纸搜索页面 -->
    <el-dialog :title="title" :visible.sync="viewOpen" width="1200px" append-to-body>
      <view-line></view-line>
    </el-dialog>

  </div>
</template>

<script>
import { listDrawing, getDrawing, delDrawing, addDrawing, updateDrawing } from "@/api/mes/md/drawing";
import { treeSelect } from "@/api/mes/md/drawingType";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import ItemSelect  from "@/components/itemSelect/single.vue";
import drawingLine from "./line.vue";
import viewLine from "./view.vue";

export default {
  name: "Drawing",
  components: { Treeselect, ItemSelect, drawingLine, viewLine },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 图纸集管理表格数据
      drawingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        drawingName: null,
        version: null,
        itemId: null,
        itemCode: null,
        itemName: null,
        itemDrawCode: null,
        specification: null,
        unitOfMeasure: null,
        drawingTypeId: null,
        drawingTypeCode: null,
        drawingTypeName: null,
        processAuthorization: null,
        bomAuthorization: null,
        status: null,
        designer: null,
        designDate: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null
      },
      // 表单参数
      form: {},
      // 图纸分类名称
      drawingTypeName: undefined,
      // 图纸分类树选项
      drawingTypeOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      optType: undefined,
      // 图纸搜索开关
      viewOpen: false,
      // 表单校验
      rules: {
        drawingName: [
          { required: true, message: "产品图纸集名称不能为空", trigger: "blur" }
        ],
        version: [
          { required: true, message: "图纸集版本号不能为空", trigger: "blur" }
        ],
        itemId: [
          { required: true, message: "关联产品ID不能为空", trigger: "blur" }
        ],
        itemCode: [
          { required: true, message: "关联产品编码不能为空", trigger: "blur" }
        ],
        itemName: [
          { required: true, message: "关联产品名称不能为空", trigger: "blur" }
        ],
        itemDrawCode: [
          { required: true, message: "关联产品图号不能为空", trigger: "blur" }
        ],
        drawingTypeId: [
          { required: true, message: "图纸分类ID不能为空", trigger: "blur" }
        ],
        drawingTypeCode: [
          { required: true, message: "图纸分类编码不能为空", trigger: "blur" }
        ],
        drawingTypeName: [
          { required: true, message: "图纸分类名称不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "图纸集状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  watch: {
    // 根据名称筛选分类树
    drawingTypeName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getTreeSelect();
  },
  methods: {
    /** 查询图纸集管理列表 */
    getList() {
      this.loading = true;
      listDrawing(this.queryParams).then(response => {
        this.drawingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        drawingName: null,
        version: null,
        itemId: null,
        itemCode: null,
        itemName: null,
        itemDrawCode: null,
        specification: null,
        unitOfMeasure: null,
        drawingTypeId: null,
        drawingTypeCode: null,
        drawingTypeName: null,
        processAuthorization: 0,
        bomAuthorization: 0,
        status: 0,
        remark: null,
        designer: null,
        designDate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getTreeSelect();
      if (this.queryParams.drawingTypeId != 0) {
        this.form.drawingTypeId = this.queryParams.drawingTypeId;
      }
      this.optType = "add";
      this.open = true;
      this.title = "添加图纸集管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeSelect();
      const id = row.id
      getDrawing(id).then(response => {
        this.form = response.data;
        this.optType = "edit";
        this.open = true;
        this.title = "修改图纸集管理";
      });
    },
    handleView(row) {
      this.reset();
      getDrawing(row.id).then(response => {
        this.form = response.data;
        this.optType = "view";
        this.open = true;
        this.title = "查看图纸集管理";
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDrawing(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDrawing(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      this.$modal.confirm('是否确认删除图纸集管理编号为"' + ids + '"的数据项？').then(function() {
        return delDrawing(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/drawing/export', {
        ...this.queryParams
      }, `drawing_${new Date().getTime()}.xlsx`)
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.drawingTypeId = data.id;
      this.handleQuery();
    },
    // 获取图纸分类树
    getTreeSelect() {
      treeSelect().then(response => {
        this.drawingTypeOptions = response.data;
      })
    },
    // 产品物料选择开关
    handleSelectProduct(){
      this.$refs.itemSelect.showFlag = true;
    },
    // 产品物料选择弹出框
    onItemSelected(obj){
      if(obj != undefined && obj != null){
        this.form.itemId = obj.itemId;
        this.form.itemCode = obj.itemCode;
        this.form.itemName = obj.itemName;
        this.form.unitOfMeasure = obj.unitOfMeasure;
        this.form.specification = obj.specification;
        this.form.itemDrawCode = obj.drawNumber;
      }
    },
    // 工艺授权开关
    handleProcessAuthorizationChange(row) {
      updateDrawing(row).then(response => {
        this.$modal.msgSuccess("工艺授权成功");
        this.getList();
      });
    },
    // BOM授权开关
    handleBomAuthorizationChange(row) {
      updateDrawing(row).then(response => {
        this.$modal.msgSuccess("BOM授权成功");
        this.getList();
      });
    },
    // 启用
    handleStart(row) {
      row.status = 1;
      updateDrawing(row).then(response => {
        this.$modal.msgSuccess("图纸集" + row.drawingName + "启用成功！");
        this.getList();
      });
    },
    // 禁用
    handleStop(row) {
      row.status = 2;
      updateDrawing(row).then(response => {
        this.$modal.msgSuccess("图纸集" + row.drawingName + "禁用成功！");
        this.getList();
      });
    },
    // 图纸搜索
    handleQueryLine() {
      this.title = "图纸搜索";
      this.viewOpen = true;

    },
    // 查看图纸集使用情况
    handleViewUsed(row) {

    },
  }
};
</script>
<style scoped>
.custom-divider ::v-deep .el-divider__text {
  background-color: #fff;
  color: #409EFF;
  padding: 4px 12px;
  border-radius: 8px;
  border: 1px solid #409EFF;
}
.custom-divider{
  background-color: #409EFF;
}
</style>

